package heroes;

import game.Square;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JPanel;

public class HeroDisplayer extends JPanel {
	private Hero myHero;

	public HeroDisplayer(Hero toBlinked) {
		myHero = toBlinked;
		clickable = false;
		clicked = false;
	}

	public static int DISPLAY_EDGE;
	public static int DISPLAY_WIDTH;
	public static int DISPLAY_HEIGHT;
	public static int FONT_HEIGHT = 12;
	public static Font pandinFont = new Font("Dialog", Font.BOLD, 12);
	public boolean clickable;
	public boolean clicked;

	public static void initPreferredSize() {
		DISPLAY_EDGE = 2;
		DISPLAY_WIDTH = Square.SQUARE_WIDTH - 2 * DISPLAY_EDGE;
		DISPLAY_HEIGHT = Square.SQUARE_HEIGHT - 2 * DISPLAY_EDGE;

	}

	// TODO the player's hero will be showed with a blue edge to make it more
	// obvious.
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Color originColor = g.getColor();
		g.setColor(Color.GREEN);
		g.drawRect(DISPLAY_EDGE, DISPLAY_EDGE, DISPLAY_WIDTH, DISPLAY_HEIGHT);
		// TODO g.setFont
		// if (FONT_HEIGHT == 0) {
		// FONT_HEIGHT = g.getFont().getSize();
		// System.out.println("FONT_HEIGHT "+ FONT_HEIGHT);
		// }
		g.setColor(Color.BLACK);
		g.drawString(
				myHero.getController().getName() + " : " + myHero.getName(),
				DISPLAY_EDGE + 5, FONT_HEIGHT + DISPLAY_EDGE + 5);
		// we use handCardNum here because handCards will not be known by any
		// clientModel
		g.drawString("Card:" + myHero.handCardNum + "       " + "HP:" + myHero.hp
				+ " /" + myHero.hpLimit, DISPLAY_EDGE + 5, DISPLAY_EDGE
				+ DISPLAY_HEIGHT / 6 + 5);
		equipmentDisplay(g);
		pandinDisplay(g);
		g.setColor(originColor);
	}

	public void equipmentDisplay(Graphics g) {
		if (myHero.weaponEquipped) {
			g.setColor(Color.BLACK);
//			if(myHero.weapon == null){
//				try {
//					Thread.sleep(3);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}				
			g.drawString(
					" " + myHero.weapon.rangeNum + " "
							+ myHero.weapon.getName(), DISPLAY_EDGE + 5,
					DISPLAY_EDGE +DISPLAY_HEIGHT/3 + 5);
			if (myHero.weapon.getColor() == 1) {
				g.setColor(Color.RED);
				g.drawString("♥" + myHero.weapon.getNumber(),
						DISPLAY_EDGE + 80, DISPLAY_EDGE + 
								 DISPLAY_HEIGHT/3 + 5);
			} else if (myHero.weapon.getColor() == 2) {
				g.setColor(Color.RED);
				g.drawString("♦" + myHero.weapon.getNumber(),
						DISPLAY_EDGE + 80, DISPLAY_EDGE + 
								DISPLAY_HEIGHT/3 + 5);
			} else if (myHero.weapon.getColor() == 3) {
				g.setColor(Color.BLACK);
				g.drawString("♠" + myHero.weapon.getNumber(),
						DISPLAY_EDGE + 80, DISPLAY_EDGE + 
								 DISPLAY_HEIGHT/3 + 5);
			} else if (myHero.weapon.getColor() == 4) {
				g.setColor(Color.BLACK);
				g.drawString("♣" + myHero.weapon.getNumber(),
						DISPLAY_EDGE + 80, DISPLAY_EDGE + 
								DISPLAY_HEIGHT/3 + 5);
			}
			g.setColor(Color.BLACK);
		}
		if (myHero.armorEquipped) {
			g.setColor(Color.BLACK);
			g.drawString("   " + myHero.armor.getName(), DISPLAY_EDGE + 5,
					DISPLAY_EDGE + DISPLAY_HEIGHT/2 + 5);
			if (myHero.armor.getColor() == 1) {
				g.setColor(Color.RED);
				g.drawString("♥" + myHero.armor.getNumber(), DISPLAY_EDGE + 80,
						DISPLAY_EDGE + DISPLAY_HEIGHT /2+ 5);
			} else if (myHero.armor.getColor() == 2) {
				g.setColor(Color.RED);
				g.drawString("♦" + myHero.armor.getNumber(), DISPLAY_EDGE + 80,
						DISPLAY_EDGE +DISPLAY_HEIGHT/2 + 5);
			} else if (myHero.armor.getColor() == 3) {
				g.setColor(Color.BLACK);
				g.drawString("♠" + myHero.armor.getNumber(), DISPLAY_EDGE + 80,
						DISPLAY_EDGE +  DISPLAY_HEIGHT /2+ 5);
			} else if (myHero.armor.getColor() == 4) {
				g.setColor(Color.BLACK);
				g.drawString("♣" + myHero.armor.getNumber(), DISPLAY_EDGE + 80,
						DISPLAY_EDGE + DISPLAY_HEIGHT/2 + 5);
			}
			g.setColor(Color.BLACK);
		}
		if (myHero.plusHorseEquipped) {
			g.setColor(Color.BLACK);
			g.drawString("+1 " + myHero.plusHorse.getName(), DISPLAY_EDGE + 5,
					DISPLAY_EDGE + DISPLAY_HEIGHT*2/3 + 5);
			if (myHero.plusHorse.getColor() == 1) {
				g.setColor(Color.RED);
				g.drawString("♥" + myHero.plusHorse.getNumber(),
						DISPLAY_EDGE + 80, DISPLAY_EDGE + 
								DISPLAY_HEIGHT*2/3 + 5);
			} else if (myHero.plusHorse.getColor() == 2) {
				g.setColor(Color.RED);
				g.drawString("♦" + myHero.plusHorse.getNumber(),
						DISPLAY_EDGE + 80, DISPLAY_EDGE +DISPLAY_HEIGHT *2/3+ 5);
			} else if (myHero.plusHorse.getColor() == 3) {
				g.setColor(Color.BLACK);
				g.drawString("♠" + myHero.plusHorse.getNumber(),
						DISPLAY_EDGE + 80, DISPLAY_EDGE +DISPLAY_HEIGHT*2/3+ 5);
			} else if (myHero.plusHorse.getColor() == 4) {
				g.setColor(Color.BLACK);
				g.drawString("♣" + myHero.plusHorse.getNumber(),
						DISPLAY_EDGE + 80, DISPLAY_EDGE + DISPLAY_HEIGHT*2/3 + 5);
			}
			g.setColor(Color.BLACK);
		}
		if (myHero.minusHorseEquipped) {
			g.setColor(Color.BLACK);
			g.drawString("-1 " + myHero.minusHorse.getName(), DISPLAY_EDGE + 5,
					DISPLAY_EDGE +DISPLAY_HEIGHT* 5 / 6  + 5);
			if (myHero.minusHorse.getColor() == 1) {
				g.setColor(Color.RED);
				g.drawString("♥" + myHero.minusHorse.getNumber(),
						DISPLAY_EDGE + 80, DISPLAY_EDGE +  DISPLAY_HEIGHT* 5 / 6 + 5);
			} else if (myHero.minusHorse.getColor() == 2) {
				g.setColor(Color.RED);
				g.drawString("♦" + myHero.minusHorse.getNumber(),
						DISPLAY_EDGE + 80, DISPLAY_EDGE +DISPLAY_HEIGHT * 5 / 6+ 5);
			} else if (myHero.minusHorse.getColor() == 3) {
				g.setColor(Color.BLACK);
				g.drawString("♠" + myHero.minusHorse.getNumber(),
						DISPLAY_EDGE + 80, DISPLAY_EDGE + DISPLAY_HEIGHT* 5 / 6 + 5);
			} else if (myHero.minusHorse.getColor() == 4) {
				g.setColor(Color.BLACK);
				g.drawString("♣" + myHero.minusHorse.getNumber(),
						DISPLAY_EDGE + 80, DISPLAY_EDGE +DISPLAY_HEIGHT * 5 / 6+ 5);
			}
			g.setColor(Color.BLACK);
		}
	}

	public void pandinDisplay(Graphics g) {
		g.setColor(Color.RED);
		g.setFont(pandinFont);// TODO better change this into a special font
		if(myHero.link!= null){
			g.drawString("LINK", DISPLAY_EDGE + 45, DISPLAY_EDGE + DISPLAY_HEIGHT
					- 5);
		}
		if (myHero.ifLeBu) {
			g.drawString("Le", DISPLAY_EDGE + 80, DISPLAY_EDGE + DISPLAY_HEIGHT
					- 5);
		}
		if (myHero.ifBinLiang) {
			g.drawString("Bin", DISPLAY_EDGE + 100, DISPLAY_EDGE
					+ DISPLAY_HEIGHT - 5);
		}
		if (myHero.ifShanDian) {
			g.drawString("Shan", DISPLAY_EDGE + 120, DISPLAY_EDGE
					+ DISPLAY_HEIGHT - 5);
		}
	}
}
